/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.projectdemo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.service.CrudService;
import com.foreveross.webbase.projectdemo.dao.ProjectapplyDao;
import com.foreveross.webbase.projectdemo.entity.PPt;
import com.foreveross.webbase.projectdemo.entity.Projectapply;
import com.foreveross.webbase.projectdemo.service.PPtService;
import com.foreveross.webbase.projectdemo.service.ProjectapplyService;
import com.foreveross.webbase.projectdemo.service.ServertypeService;
import com.foreveross.webbase.system.sys.utils.UserUtils;
import com.google.common.collect.Lists;

/**
 * 项目管理Service
 * @author yangwanli
 * @version 2017-03-15
 */
@Service
public class ProjectapplyServiceImpl extends CrudService<ProjectapplyDao, Projectapply> implements ProjectapplyService {

	@Autowired
	private PPtService pPtService;
	
	@Autowired
	private ServertypeService servertypeService;
	
	/**
	 * 获取单条数据
	 * @param id
	 * @return
	 */
	@Override
	public Projectapply get(String id) {
		//获取Projectapply的单条数据
		Projectapply py = dao.get(id);
		PPt pt = new PPt();
		pt.setProjectid(py.getId());
		//根据projectid在中间表查询对应的typeid集合
		List<PPt> ptList=pPtService.findList(pt);
		//遍历ptList集合将typeid添加到typeidList
		List<String> typeList = Lists.newArrayList();
		List<String> typeidList =  Lists.newArrayList();
		for(PPt ppt : ptList){
			typeList.add(servertypeService.get(ppt.getTypeid()).getType());
			typeidList.add(ppt.getTypeid());
		}
		py.setTypeList(typeList);
		py.setTypeidList(typeidList);
		return py;
	}
	
	/**
	 * 获取单条数据
	 * @param entity
	 * @return
	 */
	@Override
	public Projectapply get(Projectapply projectapply) {
		Projectapply py = dao.get(projectapply);
		PPt pt = new PPt();
		pt.setProjectid(py.getId());
		//根据projectid在中间表查询对应的typeid集合
		List<PPt> ptList=pPtService.findList(pt);
		//遍历ptList集合将typeid添加到typeidList
		List<String> typeList = Lists.newArrayList();
		List<String> typeidList =  Lists.newArrayList();
		for(PPt ppt : ptList){
			typeList.add(servertypeService.get(ppt.getTypeid()).getType());
			typeidList.add(ppt.getTypeid());
		}
		py.setTypeList(typeList);
		py.setTypeidList(typeidList);
		return py;
	}
	
	/**
	 * 查询列表数据
	 * @param entity
	 * @return
	 */
	@Override
	public List<Projectapply> findList(Projectapply projectapply) {
		List<Projectapply> pyList=dao.findList(projectapply);
		for(Projectapply py : pyList){
			PPt pt = new PPt();
			pt.setProjectid(py.getId());
			//根据projectid在中间表查询对应的typeid集合
			List<PPt> ptList=pPtService.findList(pt);
			//遍历ptList集合将typeid添加到typeidList
			List<String> typeList = Lists.newArrayList();
			List<String> typeidList =  Lists.newArrayList();
			for(PPt ppt : ptList){
				typeList.add(servertypeService.get(ppt.getTypeid()).getType());
				typeidList.add(ppt.getTypeid());
			}
			py.setTypeList(typeList);
			System.out.println("========================================"+typeList.toString());
			py.setTypeidList(typeidList);
		}
		return dao.findList(projectapply);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param entity
	 * @return
	 */
	@Override
	public Page<Projectapply> findPage(Page<Projectapply> page, Projectapply projectapply) {
		projectapply.setPage(page);
		List<Projectapply> pyList=dao.findList(projectapply);
		for(Projectapply py : pyList){
			PPt pt = new PPt();
			pt.setProjectid(py.getId());
			//根据projectid在中间表查询对应的typeid集合
			List<PPt> ptList=pPtService.findList(pt);
			//遍历ptList集合将typeid添加到typeidList
			List<String> typeList = Lists.newArrayList();
			List<String> typeidList =  Lists.newArrayList();
			for(PPt ppt : ptList){
				typeList.add(servertypeService.get(ppt.getTypeid()).getType());
				typeidList.add(ppt.getTypeid());
			}
			py.setTypeList(typeList);
			System.out.println("========================================"+typeList.toString());
			py.setTypeidList(typeidList);
		}
		page.setList(pyList);
		return page;
	}
	
	/**
	 * 获取单条数据(根据项目名称来查询)
	 */
	public Projectapply getName(String name) {
		return dao.getName(name);
	}
	
	/**
	 * 删除数据
	 * @param id
	 */
	@Override
	@Transactional(readOnly = false)
	public void delete(String id) {
		PPt pt=new PPt();
		pt.setProjectid(id);
		//同步删除中间表信息
		pPtService.delete(pt);
		dao.delete(id);
	}		

	/**
	 * 只是用来修改status(状态)字段
	 * @param projectapply
	 */
	public void saveUpdate(Projectapply projectapply){
		if (!projectapply.getIsNewRecord()){
			projectapply.preUpdate();
			dao.saveUpdate(projectapply);
		}
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param entity
	 */
	@Override
	@Transactional(readOnly = false)
	public void save(Projectapply projectapply) {
		if (projectapply.getIsNewRecord()){
			projectapply.preInsert();
			projectapply.setUserid(UserUtils.getUser().getId());
			System.out.println(projectapply.getUserid()+"--------------------------------------------------------");
			dao.insert(projectapply);
		}else{
			projectapply.preUpdate();
			dao.update(projectapply);
		}
		PPt pt=new PPt();
		pt.setProjectid(projectapply.getId());
		//删除类型与申请表之间的中间的纪录
		pPtService.deleteEntity(pt);
		List<String> typeidList=projectapply.getTypeidList();
		for(String str : typeidList){
			PPt ppt=new PPt();
			ppt.setProjectid(projectapply.getId());
			ppt.setTypeid(str);
			//在类型与申请表之间的中间表添加纪录
			pPtService.save(ppt);
		}
	}
}